import { AmbientLight, DirectionalLight, Vector3 } from "three";
import { Service, App } from "vjmap3d";
import "vjmap3d/dist/vjmap3d.min.css";
import { env } from "./env";
export const createApp = (containerID: string) => {
    let svc = new Service(env.serviceUrl, env.accessToken);
    if (env.workspace) {
        // 如果有工作区，切换至相对应的工作区
        svc.switchWorkspace(env.workspace)
    }
    let app = new App(svc, {
        container: containerID,
        render: {
            // @ts-ignore
            alpha: true,
            powerPreference: "default",
            antialias: true,
            logarithmicDepthBuffer: true
            //  stencil: false,
            //   depth: false
            // clearColorAlpha: 0
        },
        stat: {
            show: true,
            left: "0"
        },
        scene: {
            showAxesHelper: false,
            axesHelperSize: 1,
            defaultLights: false,
            gridHelper: {
                visible: env.gridHelper ?? true,
                //side: DoubleSide,

                // followCamera: true,
                args: [1000, 1000],
                cellSize: 10,
                sectionSize: 100
            }
            // roomEnvironment: true,
        },
        ui: {
            configPane: {
                title: "配置",
                //
                style: {
                    right: "5px",
                    top: "10px"
                    //width: "300px"
                }
            }
        },
        coordSystem: {
            //  centerOrigin: new Vector3(587617000.8339591, 3103899303.1920567, 0)
        },
        // postProcess: {
        //     enable: true
        // },
        control: {
            initState: {
                cameraTarget: new Vector3(0.34938223205517505,-1.2567614031146929e-18, 0.6245268264422921),
				cameraPosition: new Vector3(0.35048876379780275,31.798749151802628, 10.339706039907002),
            },
            //unProjectUseDepth: false,
            //dblClickSetToCenter: false
            leftButtonPan: true,
            // verticalDragToForward: true
            // eventFromInputModule: true
        },
        camera: {
            isOrthographicCamera: false,
            fov: 45,
            near: 0.05,
            far: 1000,
            autoNearFar: false,
            viewHelper: {
                enable: true,
                position: "leftBottom"
            }
        },
        postProcess: {
            enable: true,
            useFXAA: true,
            useSMAA: false,
            disableMergeEffect: false
        }
    });

    // 增加光
    const directionalLight = new DirectionalLight(0xffffff, 1);
    directionalLight.position.set(400, 300, 300);
    app.scene.add(directionalLight);
   
    const directionalLight2 = new DirectionalLight(0xffffff, 2);
    directionalLight2.position.set(-400, -300, -300);
    app.scene.add(directionalLight2);
    
    //环境光
    const ambient = new  AmbientLight(0xffffff, 0.8);
    app.scene.add(ambient);
    return app
}